package com.example.view;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.example.tool.CrashHandler;

import java.io.File;

public class WelcomeActivity extends Activity {
	private ImageView iv_main;
	private CountDownTimer count;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,      
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
		setContentView(R.layout.activity_welcome);

		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());
		Thread.setDefaultUncaughtExceptionHandler(crashHandler);

		initView();
//		initData();
		verifyStoragePermissions(WelcomeActivity.this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	private void initView() {
		iv_main = (ImageView) findViewById(R.id.iv_main);
		iv_main.setImageResource(R.drawable.default_1920_english);
	}

	private void initData() {
		count = new CountDownTimer(1 * 1010, 1000) {
			@Override
			public void onTick(final long arg0) {
			}

			@Override
			public void onFinish() {
				runOnUiThread(new Runnable() {
					public void run() {
						startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
						finish();
					}
				});
			}
		};
		count.start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		iv_main.setBackgroundResource(0);
		iv_main.setImageBitmap(null);
		iv_main.setImageDrawable(null);
	}

	private static final String[] PERMISSION_EXTERNAL_STORAGE = new String[] {
			Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
			Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
			Manifest.permission.CHANGE_NETWORK_STATE, Manifest.permission.WRITE_SETTINGS,
	};
	private static final int REQUEST_EXTERNAL_STORAGE = 100;

	private void verifyStoragePermissions(Activity activity) {
		int permissionWrite = ActivityCompat.checkSelfPermission(activity,
				Manifest.permission.CHANGE_NETWORK_STATE);
		if (permissionWrite != PackageManager.PERMISSION_GRANTED) {
			ActivityCompat.requestPermissions(activity, PERMISSION_EXTERNAL_STORAGE,
					REQUEST_EXTERNAL_STORAGE);
		} else {
			initData();
		}
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		switch (requestCode) {
			case REQUEST_EXTERNAL_STORAGE:
				initData();
				break;
		}
	}
}
